/**
 * 材料
 * Created by douxc on 2015/4/21.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller('MaterialCtrl', ['$scope', '$routeParams', 'MHService','$location',
      function ($scope, $routeParams, MHService,$location) {
        $scope.showSubmit = ($routeParams['flag'] === 'add');



          var supplier = MHService.globalData(false, {key: 'supplier'});
          var isShow = MHService.globalData(false, {key: 'isShow'});
          isShow = JSON.parse(isShow);
//          console.log(isShow,typeof(isShow));
          //将选中的供应商数据传过来
          if(supplier&&isShow){
              $scope.selected = angular.copy(supplier);
//              console.log('16',supplier);
              $scope.feeds = undefined;
              $scope.search = undefined;
//              if (supplier.images) {
//                $scope.images = supplier.images.split(',');
//             }
          }
        /**
         * 监听输入框
         */
        $scope.$watch('search', function () {
          //name变化时
          if ($scope.search && $scope.search.length > 0) {
            MHService.postReq({url: MHService.URL.fees_search + '?key=' + $scope.search}, function (data) {
              $scope.feeds = data['datas'];
            });
          }
        });
        /**
         * 选中的材料
         * @param feed
         */
        $scope.feedSlected = function (feed) {
//          $scope.selected = angular.copy(feed);
//          $scope.feeds = undefined;
//          $scope.search = undefined;
          //图片
//          if (feed[11]) {
//            $scope.images = feed[11].split(',');
//            console.info($scope.images);
//          }
//            console.log(feed);
            //将当前材料对象保存，接下来会用到
            MHService.globalData(true, {key: 'feed', value : feed});
            $location.path("/supplierList");
        };
        /**
         * 保存选中的材料
         * @param selected
         */
        $scope.saveFeed = function (selected) {
          if ($scope.num && selected.relation.price && selected.relation.pprice) {
              var price = selected.relation.price+selected.relation.pprice;//材料费+人工费
//              var select = [];
//            select.push({name:selected.name,num: $scope.num, price: price});//数量
              var select = {
                  name:selected.name,
                  num: $scope.num,
                  price: price
              };
            //var submit = [selected[0], selected.num, selected.time].join(','),//最终提交的数据
            //  showData = {id: selected[0], name: selected[1], time: selected[7]};//展示在材料列表的数据
            var _value = MHService.cacheData(false, {key: 'materialList'});//取出已有的材料
//              console.log(select);
            if (_value) {
              //已有材料
              _value.push(select);
              MHService.cacheData(true, {key: 'materialList', value: _value});
            } else {
              //没有值，直接保存
              MHService.cacheData(true, {key: 'materialList', value: [select]});
            }
            MHService.Toast('添加成功');
//            history.go(-1);//回退
              $location.path("/quote");
          } else {
            MHService.Toast('请检查材料费、人工费和数量');
          }
        };
      }]);
  };
});
